home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 441 / dlibs12 / splitpat.c < prev    next >
C/C++ Source or Header  |  1990-11-23  |  2KB  |  99 lines

  1. #include <stddef.h>
  2. #include <string.h>
  3.  
  4. #define    SPLIT_EXT    1
  5. #define    SPLIT_FILE    2
  6. #define    SPLIT_PATH    3
  7.  
  8. char *_splitpath(src, drive, path, file, ext)
  9.     char *src, *drive, *path, *file, *ext;
  10.     {
  11.     register int state = SPLIT_EXT;
  12.     register char *q;
  13.     char buf[128];
  14.  
  15.     if(drive)
  16.         *drive = '\0';
  17.     if(path)
  18.         *path = '\0';
  19.     if(file)
  20.         *file = '\0';
  21.     if(ext)
  22.         *ext = '\0';
  23.     if(src == NULL)
  24.         return(NULL);
  25.     strcpy(buf, src);
  26.     q = strrchr(buf, '\0');
  27.     while((q--) > buf)
  28.         {
  29.         if(*q == '.')
  30.             {
  31.             if(state < SPLIT_FILE)
  32.                 {
  33.                 state = SPLIT_FILE;
  34.                 if(ext)
  35.                     strcpy(ext, q+1);
  36.                 *q = '\0';
  37.                 }
  38.             }
  39.         else if(*q == '\\')
  40.             {
  41.             if(state < SPLIT_PATH)
  42.                 {
  43.                 state = SPLIT_PATH;
  44.                 if(file)
  45.                     strcpy(file, q+1);
  46.                 if((q == buf) || (*(q-1) == ':'))
  47.                     *(q+1) = '\0';
  48.                 else
  49.                     *q = '\0';
  50.                 }
  51.             }
  52.         else if(*q == ':')
  53.             break;
  54.         }
  55.     if(state < SPLIT_PATH)
  56.         {
  57.         if(file)
  58.             strcpy(file, q+1);
  59.         }
  60.     else
  61.         {
  62.         if(path)
  63.             strcpy(path, q+1);
  64.         }
  65.     *(q+1) = '\0';
  66.     if(drive)
  67.         strcpy(drive, buf);
  68.     return(src);
  69.     }
  70.  
  71. char *_makepath(dst, drive, path, file, ext)
  72.     register char *dst;
  73.     char *drive, *path, *file, *ext;
  74.     {
  75.     register char *p;
  76.  
  77.     if(dst)
  78.         {
  79.         *dst = '\0';
  80.         if(drive && *drive)
  81.             strcat(dst, drive);
  82.         if(path && *path)
  83.             {
  84.             strcat(dst, path);
  85.             p = strrchr(dst, '\0');
  86.             if(*(p - 1) != '\\')
  87.                 strcat(dst, "\\");
  88.             }
  89.         if(file && *file)
  90.             strcat(dst, file);
  91.         if(ext && *ext)
  92.             {
  93.             strcat(dst, ".");
  94.             strcat(dst, ext);
  95.             }
  96.         }
  97.     return(dst);
  98.     }
  99.